/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import entity.Cliente;
import entity.ContaInvest;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author Tiago
 */
public class ContaInvestimentoDAO {

    private Connection connection;

    public ContaInvestimentoDAO() {
        this.connection = new ConnectionFactory().getConnection();
    }

    public void insere(int codCliente, ContaInvest investimento) throws SQLException {
        String sql1 = "INSERT INTO conta (idCliente, saldo )  VALUES (?, ?)";
        String sql2 = "INSERT INTO containvestimento(montanteMin, depositoMin, depositoInicial, fkconta) "
                + "values(?, ?, ?, ?) ";
        String sql3 = "SELECT numConta FROM CONTA WHERE idCliente =" + codCliente + "";
        PreparedStatement stmt1 = connection.prepareStatement(sql1);
        PreparedStatement stmt2 = connection.prepareStatement(sql2);
        PreparedStatement stmt3 = connection.prepareStatement(sql3);
        stmt1.setInt(1, codCliente);
        stmt1.setDouble(2, investimento.getDepositoInicial());
        stmt1.executeUpdate();
        ResultSet rs = stmt3.executeQuery();
        int fkconta = 0;
        while (rs != null && rs.next()) {
            fkconta = rs.getInt("numConta");
        }
        stmt2.setDouble(1, investimento.getMontanteMinimo());
        stmt2.setDouble(2, investimento.getDepositoMinimo());
        stmt2.setDouble(3, investimento.getDepositoInicial());
        stmt2.setInt(4, fkconta);
        stmt2.executeUpdate();
    }

    public void deposita(Double valor, int idCliente) throws SQLException {
        String sql = "UPDATE conta SET saldo = (saldo+?) WHERE idCliente = ?";
        PreparedStatement stmt1 = connection.prepareStatement(sql);
        stmt1.setDouble(1, valor);
        stmt1.setInt(2, idCliente);
        stmt1.executeUpdate();
        //return true;
    }
    
    public void saca(Double valor, int idCliente) throws SQLException {
        String sql = "UPDATE conta SET saldo = (saldo-?) WHERE idCliente = ?";
        PreparedStatement stmt1 = connection.prepareStatement(sql);
        stmt1.setDouble(1, valor);
        stmt1.setInt(2, idCliente);
        stmt1.executeUpdate();
        //return true;
    }

//    public int getCliente(int id) {
//        String sql = "SELECT idCliente FROM "
//    }

}
